package com.woniuxy.relation.service.service.impl;

import java.time.LocalDateTime;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniuxy.commons.util.WebUtil;
import com.woniuxy.relation.common.RelationResultCode;
import com.woniuxy.relation.common.exception.RelationException;
import com.woniuxy.relation.infra.mapper.AccountRelationDao;
import com.woniuxy.relation.infra.dao.model.AccountRelation;
import com.woniuxy.relation.service.service.AccountRelationService;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author huanglei
 * @since 2022-08-01
 */
@Service
public class AccountRelationServiceImpl extends ServiceImpl<AccountRelationDao, AccountRelation> implements AccountRelationService {


    /*
     *
     *
     * */
    @Override
    public void attentionScreen(String followedAccountId) {
        //token中的数据
        Integer tokenId = 1;
        AccountRelation accountRelation = new AccountRelation();
        accountRelation.setFocusonAccountId(tokenId);
        accountRelation.setFollowedAccountId(Integer.parseInt(followedAccountId));
        accountRelation.setFocusOnTime(LocalDateTime.now());
        accountRelation.setFocusOnState(0);


        //不存在关注关系
        AccountRelation accountRelationDB = this.getOne(new QueryWrapper<AccountRelation>().eq("focuson_account_id", 1).eq("followed_account_id", Integer.parseInt(followedAccountId)));
        if (accountRelationDB != null) {
            if (accountRelationDB.getFocusOnState() == 0) {
                throw new RelationException(RelationResultCode.IS_FACOUSON.getCode(), RelationResultCode.IS_FACOUSON.getMessage());
            } else {
                UpdateWrapper<AccountRelation> updateWrapper = new UpdateWrapper<AccountRelation>().eq("id", accountRelationDB.getId());
                this.saveOrUpdate(accountRelation, updateWrapper);
            }
        } else {
            this.saveOrUpdate(accountRelation);
        }


    }


    /*
      * @Description:
       * @param accountId
      * @Author: 黄磊
      * @Date: 2022/8/1 17:57
      */
    @Override
    public void cancelScreen(String accountId) {

        Integer tokenId = 1;
        AccountRelation accountRelationDB = this.getOne(new QueryWrapper<AccountRelation>().eq("focuson_account_id", tokenId).eq("followed_account_id", Integer.parseInt(accountId)));
        if (accountRelationDB == null) {
            throw new RelationException(RelationResultCode.IS_NOTEXIST.getCode(), RelationResultCode.IS_NOTEXIST.getMessage());
        }
        if (accountRelationDB.getFocusOnState() == 1) {
            throw new RelationException(RelationResultCode.IS_CANCEL.getCode(), RelationResultCode.IS_CANCEL.getMessage());
        }

        UpdateWrapper<AccountRelation> updateWrapper = new UpdateWrapper<AccountRelation>().eq("id", accountRelationDB.getId()).set("focus_on_state", 1).set("cancel_focus_time",LocalDateTime.now());
        this.saveOrUpdate(accountRelationDB, updateWrapper);
    }


}
